package game.module.ddz.processor;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import game.common.PlayingRoleMsgProcessor;
import game.entity.PlayingRole;
import game.module.club.logic.ClubRoomManager;
import game.module.ddz.bean.DdzRoom;
import game.module.ddz.bean.DdzRoomCfg;
import game.module.ddz.dao.DdzRoomTemplateCache;
import game.module.ddz.logic.DdzDataManager;
import game.module.ddz.logic.DdzLogic;
import game.module.hall.logic.CheatManager;
import lion.common.MsgCodeAnn;
import lion.netty4.message.MyRequestMessage;
import lion.netty4.message.RequestMessageRaw;
import lion.netty4.message.RequestProtoMessage;
import lua.LuaMessageDdz.C2SCreateRoom;
import lua.LuaMessageDdz.S2CCreateRoom;
import lua.LuaMessageLogin.S2ErrorCode;

@MsgCodeAnn(msgcode = C2SCreateRoom.id, accessLimit = 500)
public class DdzCreateRoomProcessor extends PlayingRoleMsgProcessor {

	private static Logger logger = LoggerFactory.getLogger(DdzCreateRoomProcessor.class);

	@Override
	public void process(PlayingRole hero, RequestMessageRaw request) throws Exception {
		C2SCreateRoom reqMsg = C2SCreateRoom.parse(request);
		byte jiao_or_qiang = reqMsg.jiao_or_qiang;
		int cfg_index = reqMsg.cfg_index;
		byte prevent_cheat = reqMsg.anti_cheet;
		int playerId = hero.getId();
		logger.info("ddz create room,playeyId={},jiao_or_qiang={},cfg_index={},anti_cheet={}", playerId, jiao_or_qiang, cfg_index, prevent_cheat);
		if (!DdzRoomTemplateCache.getInstance().containsConfigIndex(cfg_index)) {
			S2ErrorCode respMsg = new S2ErrorCode(101);
			hero.writeAndFlush(respMsg.build(hero.alloc()));
			return;
		}
		// 是否在房间中
		boolean isInRoom = DdzDataManager.getInstance().isPlayerInRoom(playerId);
		if (isInRoom) {
			S2ErrorCode respMsg = new S2ErrorCode(102);
			hero.writeAndFlush(respMsg.build(hero.alloc()));
			return;
		}
		// is fangka enough
		DdzRoomCfg ddzRoomCfg = DdzRoomTemplateCache.getInstance().getTemplate(cfg_index);
		int costFangka = ddzRoomCfg.getCost_fangka();
		if (hero.getPlayerBean().getFang_ka() < costFangka) {
			S2ErrorCode respMsg = new S2ErrorCode(103);
			hero.writeAndFlush(respMsg.build(hero.alloc()));
			return;
		}
		// do
		DdzRoom ddzRoom = DdzLogic.getInstance().createDdzRoom(playerId, ddzRoomCfg.getRound());
		ddzRoom.setPrevent_cheat(prevent_cheat);
		ddzRoom.setJiaoOrQiang(jiao_or_qiang);
		CheatManager.getInstance().openCheatComm(ddzRoom.getPrevent_cheat(), ddzRoom.getIps(), hero);
		ClubRoomManager.getInstance().openClubRoom(hero, ddzRoom);
		S2CCreateRoom respMsg = new S2CCreateRoom();
		respMsg.room_id = ddzRoom.getUuid();
		respMsg.round_count = ddzRoom.getRoundCount();
		respMsg.anti_cheet = prevent_cheat;
		respMsg.jiao_or_qiang = jiao_or_qiang;
		// ret
		hero.writeAndFlush(respMsg.build(hero.alloc()));
	}

	@Override
	public void processProto(PlayingRole playingRole, RequestProtoMessage request) throws Exception {
		// null
	}

	@Override
	public void processWebsocket(PlayingRole playingRole, MyRequestMessage request) throws Exception {
		// null
	}

}
